package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.StoreInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;

public class MerchantDetailActivity extends BaseActivity implements OnClickListener {
    private ImageView ivBack;
    private RelativeLayout rlLocate;
    private ImageView mStoreImage;
    private Button btnDeposit;
    private Button btnPick;
    private Button btnExchange;
    private ImageView btnCall;
    private TextView mShopName, mShopAddress, mServiceRule, mDescription;
    private final int GET_STORE_DETAIL_INFO_SUCCESS = 2;
    private StoreInfo info;
    private DBOpenHelper helper;
    private ShareStore shareStore;
    private boolean isLogin;
    private String shopId;
    private Handler handle = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case GET_STORE_DETAIL_INFO_SUCCESS:
                    info = (StoreInfo) msg.obj;
                    Glide.with(MerchantDetailActivity.this).load(info.getImage().trim()).placeholder(R.drawable.placeholder_shop_big).error(R.drawable.placeholder_shop_big).into(mStoreImage);
                    mShopName.setText(info.getShopName());
                    mShopAddress.setText(info.getAddress());
                    mServiceRule.setText(info.getTips());
                    mDescription.setText(info.getDescription());
                    if ("1".equals(info.getIsDepositSupport())) {

                    } else {
                        btnDeposit.setBackground(
                                ContextCompat.getDrawable(MerchantDetailActivity.this, R.drawable.button_invalid_bg));
                        btnDeposit.setEnabled(false);
                    }

                    if ("1".equals(info.getIsExchangeSupport())) {

                    } else {
                        btnExchange.setBackground(
                                ContextCompat.getDrawable(MerchantDetailActivity.this, R.drawable.button_invalid_bg));
                        btnExchange.setEnabled(false);
                    }
                    if ("1".equals(info.getIsExtractSupport())) {

                    } else {
                        btnPick.setBackground(
                                ContextCompat.getDrawable(MerchantDetailActivity.this, R.drawable.button_invalid_bg));
                        btnPick.setEnabled(false);
                    }
                    break;
                default:
                    break;
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_detail);
        tintManager.setStatusBarAlpha(0f);
        helper = new DBOpenHelper(MerchantDetailActivity.this);
        shareStore = new ShareStore(MerchantDetailActivity.this);
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isLogin = shareStore.getBoolean(Strs.IS_LOGIN, false);
    }

    private void initData() {
        shopId = getIntent().getStringExtra("shopId");
        ActionUtil actionUtil = new ActionUtil(MerchantDetailActivity.this);
        actionUtil.getStoreDetailInfo(shopId);
        actionUtil.setOnPassBackListener(new OnPassBackListener() {

            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()) {
                    return;
                }
                StoreInfo storeInfo = (StoreInfo) model;
                if (storeInfo == null) {
                    UIHelper.showToast(MerchantDetailActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (storeInfo.getErrorMsg() != null) {
                    UIHelper.showToast(MerchantDetailActivity.this, storeInfo.getErrorMsg());

                } else {
                    Message msg = new Message();
                    msg.what = GET_STORE_DETAIL_INFO_SUCCESS;
                    msg.obj = storeInfo;
                    handle.sendMessage(msg);
                }
            }
        });
    }

    private void initView() {

        mStoreImage = (ImageView) findViewById(R.id.shop_image);
        ivBack = (ImageView) findViewById(R.id.iv_left_arrow);
        rlLocate = (RelativeLayout) findViewById(R.id.locate_layout);
        mShopName = (TextView) findViewById(R.id.shop_name);
        mShopAddress = (TextView) findViewById(R.id.shop_address);
        mServiceRule = (TextView) findViewById(R.id.service_rule);
        mDescription = (TextView) findViewById(R.id.merchant_description);
        btnCall = (ImageView) findViewById(R.id.iv_call);
        btnDeposit = (Button) findViewById(R.id.btn_store_deposit_reserve);
        btnExchange = (Button) findViewById(R.id.btn_store_exchange_reserve);
        btnPick = (Button) findViewById(R.id.btn_store_pick_reserve);
        setListener();

    }

    private void setListener() {
        ivBack.setOnClickListener(this);
        rlLocate.setOnClickListener(this);
        btnDeposit.setOnClickListener(this);
        btnExchange.setOnClickListener(this);
        btnPick.setOnClickListener(this);
        btnCall.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left_arrow:
                finish();
                break;
            case R.id.locate_layout:
                if (info != null) {
                    ActivityUtils.gotoShopPosition(MerchantDetailActivity.this, info.getAddress(), info.getPoint().getX(),
                            info.getPoint().getY(), info.getShopName());
                }
                break;
            case R.id.btn_store_deposit_reserve:
                if (info == null) {
                    return;
                }
                if (isLogin) {
                    if (helper.getRealName().equals("")) {
                        UIHelper.showRemindDialog(MerchantDetailActivity.this,
                                getString(R.string.not_real_name_authentication_tips));
                        return;
                    }
                    ActivityUtils.gotoStoreDeposit(MerchantDetailActivity.this, info, shopId);
                } else {
                    ActivityUtils.gotoLogin(MerchantDetailActivity.this);
                }

                break;
            case R.id.btn_store_pick_reserve:
                if (info == null) {
                    return;
                }
                if (isLogin) {
                    Intent pickIntent = new Intent();
                    pickIntent.setClass(MerchantDetailActivity.this, PickGoldActivity.class);
                    Bundle bd = new Bundle();
                    bd.putSerializable("storeDetailInfo", info);
                    pickIntent.putExtra("type", "1");
                    pickIntent.putExtras(bd);
                    pickIntent.putExtra("shopId", shopId);
                    startActivity(pickIntent);
                } else {
                    ActivityUtils.gotoLogin(MerchantDetailActivity.this);
                }

                break;
            case R.id.btn_store_exchange_reserve:
                if (info == null) {
                    return;
                }
                if (isLogin) {
                    ActivityUtils.gotoStoreExchange(MerchantDetailActivity.this, info, shopId);
                } else {
                    ActivityUtils.gotoLogin(MerchantDetailActivity.this);
                }

                break;

            case R.id.iv_call:
                if (info != null) {
                    UIHelper.showCallRemindDialog(MerchantDetailActivity.this, info.getShopPhone());
                }
                break;
            default:
                break;
        }
    }

}
